home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Extracting chars from shorts.
- Date: 10 Jan 1996 20:31:15 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan10133115@qcd.lanl.gov>
- References: <4d13i0$k3s@lastactionhero.rs.itd.umich.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: dogcow@monet.ccs.itd.umich.edu's message of 10 Jan 1996 19:20:32 GMT
-
- --text follows this line--
- In article <4d13i0$k3s@lastactionhero.rs.itd.umich.edu>
- dogcow@monet.ccs.itd.umich.edu (Tom Spindler) writes:
- <snip>
- I'm trying to extracts individual bytes from a double. The following code
- doesn't work:
-
- typedef unsigned char zbyte; /* unsigned 1 byte quantity */
- typedef unsigned short zword; /* unsigned 2 byte quantity */
-
- typedef union zw_u_t {
- zword zword;
- struct {zbyte hi,lo;} zbyte;
- } zwordun;
-
- #define lo(v) ((zbyte) (((zwordun) v).zbyte.lo))
- #define hi(v) ((zbyte) (((zwordun) v).zbyte.hi))
-
- Cast to an union type is not allowed. A cast to a pointer to union
- type is however fine.
-
- main() {
- zword foon;
- zbyte zfoo;
-
- zfoo = lo(foon);
- }
-
- One compiler complains that it's an illegal cast, whereas gcc says that the
- return value is an int.
-
- I could do something like ((char *)&v[0]), but then it would crash if I
- tried to do something with lo(42).
-
- Yes. In the general case, you have to assign the value to an union (or
- whatever) and then read off the fields.
-
- Any suggestions? (I already know what endian the code is supposed to run on.)
-
- Cheers
- Tanmoy
-
-
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-